package events.containers2;

import java.util.Set;

import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;


import zerdlib.events.GenericListenable;


public class Test {
	private static class SuperInteger extends GenericListenable<DataListener<SuperInteger>> {
		public int i;

		private SuperInteger(int i) {
			super();
			this.i = i;
		}
		
		@Override
		public String toString() {
			return ""+ i;
		}
		
		public void setI(int i) {
			this.i = i;
			for(DataListener<SuperInteger> l : eventListenerList.getListeners(DataListener.class)) {
				l.onDataChange(this);
			}
		}
		
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SortedSetModel2<SuperInteger> set = new SortedSetModel2<SuperInteger>(new Comparator<SuperInteger, Object>() {
			@Override
			public int compare(SuperInteger o1, Object o2) {
				if(o1 instanceof SuperInteger && o2 instanceof SuperInteger) {
					return o1.i - ((SuperInteger)o2).i;
				} else if(o1 instanceof SuperInteger && o2 instanceof Integer) {
					return o1.i - ((Integer)o2).intValue();
				} else {
					return -1;
				}
			}
		});
		
		set.addListDataListener(new ListDataListener() {
			
			@Override
			public void intervalRemoved(ListDataEvent e) {
				System.out.println("intervalRemoved "+e.getIndex0()+","+e.getIndex1());
				print((Set<SuperInteger>)e.getSource());
			}
			
			@Override
			public void intervalAdded(ListDataEvent e) {
				System.out.println("intervalAdded "+e.getIndex0()+","+e.getIndex1());
				print((Set<SuperInteger>)e.getSource());
				
			}
			
			@Override
			public void contentsChanged(ListDataEvent e) {
				System.out.println("contentsChanged "+e.getIndex0()+","+e.getIndex1());
				print((Set<SuperInteger>)e.getSource());
				
			}
		});
		
		
		set.add(new SuperInteger(1));
		set.add(new SuperInteger(1));
		set.add(new SuperInteger(2));
		set.add(new SuperInteger(3));
		set.add(new SuperInteger(4));
		set.add(new SuperInteger(3));
		set.add(new SuperInteger(25));
		set.add(new SuperInteger(12));
		set.add(new SuperInteger(15));
		set.add(new SuperInteger(8));
		
		print(set);
		
		System.out.println(set.contains(25));
		System.out.println(set.contains(8));
		System.out.println(set.contains(1));
		
		set.remove(new Integer(25));
		set.remove(8);
		set.remove(1);
		print(set);
		
		System.out.println(set.addAll(set));
		
		Set<SuperInteger> set2 = new SortedSetModel2<SuperInteger>(new Comparator<SuperInteger, Object>() {
			@Override
			public int compare(SuperInteger o1, Object o2) {
				if(o1 instanceof SuperInteger && o2 instanceof SuperInteger) {
					return o1.i - ((SuperInteger)o2).i;
				} else if(o1 instanceof SuperInteger && o2 instanceof Integer) {
					return o1.i - ((Integer)o2).intValue();
				} else {
					return -1;
				}
			}
		});
		
		set2.add(new SuperInteger(-3));
		set2.add(new SuperInteger(101));
		set2.add(new SuperInteger(6));
		set2.add(new SuperInteger(301));
		set2.add(new SuperInteger(401));
		set2.add(new SuperInteger(301));
		set2.add(new SuperInteger(2501));
		set2.add(new SuperInteger(1201));
		set2.add(new SuperInteger(1501));
		set2.add(new SuperInteger(801));
		
		System.out.println(set.addAll(set2));
		print(set2);
		print(set);
		
		set2.clear();
		print(set2);
		print(set);
		
		set2.addAll(set);
		set2.remove(-3);
		set2.remove(2501);
		set2.remove(301);
		print(set2);
		
		System.out.println("coucou");
		
		System.out.println(set.retainAll(set2));
		print(set);
		

		set2.remove(801);
		set2.remove(6);
		
		System.out.println(set.removeAll(set2));
		print(set);
		
		set.addAll(set2);
		
		SuperInteger si = new SuperInteger(24);
		set.add(si);
		si.setI(24);
		si.setI(-15);
		si.setI(24);
		si.setI(24);
		si.setI(99999);
		si.setI(-25);
		
	}
	
	static void print(Set<SuperInteger> set) {
		System.out.print("[");
		for(SuperInteger si : set) {
			System.out.print(" "+si);
		}
		System.out.println("]");
	}

}
